//
//  UIViewController+HDRepairDetailController.m
//  app
//
//  Created by D on 15/8/31.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDRepairDetailController.h"
#import "HDRepairDetailResult.h"
#import "HDRepairDetailManager.h"
#import "HDRepairDetailCell.h"
#import "HDMakeSureView.h"
#import "HDShouFeiMIngXiAlter.h"

#import "HDPingJiaViewController.h"

#import "HDBigImageView.h"

static const CGFloat kCartTotalPanelHeight = 120;

@interface HDRepairDetailController ()<UITableViewDataSource,UITableViewDelegate,HDMakeSureViewDelegate,MBProgressHUDDelegate,HDRepairDetailDelegate>

@property (nonatomic,strong) HDRepairDetailManager * repairManager;
@property (nonatomic,strong) HDRepairDetailResult * repairResult;
@property (nonatomic,strong) UITableView * repairTableView;
@property (strong,nonatomic) MBProgressHUD* progress_;
@property (nonatomic,strong) HDShoufeimingxiResult * shoufei_request;

@end

@implementation HDRepairDetailController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"申报详情";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.repairTableView = [[UITableView alloc]initWithFrame:CGRectOf(CGPointMake(0, 64),
                                                                      [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT uh:-kCartTotalPanelHeight ]) style:UITableViewStyleGrouped];
    
    
    self.repairTableView.backgroundColor = [UIColor whiteColor];
    self.repairTableView.delegate = self;
    self.repairTableView.dataSource = self;
    
    self.repairTableView.backgroundColor = [UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1.0];
    
    [self.view addSubview:self.repairTableView];
    
    self.repairManager = [HDRepairDetailManager shareManager];
    self.repairResult = [[HDRepairDetailResult alloc]initWithTarget:self action:@selector(repairDetailResult:)];
    self.shoufei_request = [[HDShoufeimingxiResult alloc]initWithTarget:self action:@selector(shoufeiResult:)];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;

    
}

#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.model.detail.count == 0) {
        return 3;
    }else
    return 2+self.model.detail.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 7;
    }
    else{
        
        if ([self.model.status isEqualToString:@"待处理"]){
            return 6;
        
        }else
        {
            return 8;
        }
        
    }
//    return 7;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    HDRepairDetailCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HDRepairDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier Section:[indexPath section]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
   
    cell.delegate = self;
    [cell cellWithModel:self.model Section:[indexPath section] Row:indexPath.row];
    
        
    cell.backgroundView = nil;
    
    return cell;
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) {
        return 120;
    }else if ([indexPath section] ==1 && indexPath.row == 6){
    
        return 100;
    }
    else
        return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (![self.model.status isEqualToString:@"完成"]) {
        
        if (section == self.model.detail.count+2 -1 ) {
           return 64;
        }else
            return  0.01;
    }else
        return  0.01;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (![self.model.status isEqualToString:@"完成"]) {
        
        if (section == self.model.detail.count+2-1) {
            HDMakeSureView * makeSureView = [[HDMakeSureView alloc]initWithModel:self.model];
            makeSureView.delegate = self;
            return makeSureView;
        }else
            return nil;
        
    }else{
        return nil;
    }
    
    
}

-(void)makeSureCodeFinish
{
    
    self.progress_ = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.progress_];
    [self.view bringSubviewToFront:self.progress_];
    self.progress_.delegate = self;
    [self.progress_ show:YES];
    
    [self.repairManager makeSureFinish:self.repairResult Params:self.model.code];
}

-(void)repairDetailResult:(HDRepairDetailResult*)rt
{
    
    if (self.progress_) {
        [self.progress_ removeFromSuperview];
        self.progress_ = nil;
    }
    
    [MyToast showWithText:rt.message];
    
    if (rt.success) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }
}


-(void)lookShoufeimingxi:(UIButton *)sender
{

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * params = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",(long)sender.tag],@"distribute_id", nil];
    
//    NSDictionary * params = [NSDictionary dictionaryWithObjectsAndKeys:@"269",@"distribute_id", nil];
    
    [self.repairManager loadShoufeimingxiData:self.shoufei_request Params:params];
    
}



-(void)shoufeiResult:(HDShoufeimingxiResult*)rt
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (rt.success == YES) {
        
        if (rt.dataSourceArray.count > 0) {
            HDShouFeiMIngXiAlter * alter = [[HDShouFeiMIngXiAlter alloc]initWithParams:rt.dataSourceArray];
            [alter showShouFeiViewSheet];
        }else
        {
            [MyToast showWithText:rt.message];
        }
        
        
        
        
    }else
    {
         [MyToast showWithText:rt.message];
    }

}


-(void)gopingjia:(UIButton *)sender
{
    HDPingJiaViewController * vc = [[HDPingJiaViewController alloc]init];
    vc.repair_apply_id = [NSString stringWithFormat:@"%ld",(long)sender.tag];
    vc.operator_user = sender.titleLabel.text;
    
    vc.pingjia_array = self.model.evaluation_detail;
    
    [self.navigationController pushViewController:vc animated:YES];
    

}

-(void)scanBigPic:(UIButton *)sender
{
    HDBigImageView * iv = [[HDBigImageView alloc]initWithParams:sender.titleLabel.text];
    [iv showBigImageViewSheet];

}



@end












